<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\shiyao;

use app\api\model\User as UserModel;
use app\api\service\User as UserService;
use app\common\model\shiyao\Crc as CrcModel;
use app\common\model\store\Setting as SettingModel;
use cores\exception\BaseException;
use think\facade\Db;

/**
 * 模型
 * Class
 * @package app\common\model
 */
class Crc extends CrcModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	/**
	 *
	 * @return \think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws BaseException
	 */
	public function getList(array $params = []) {
		$userId = UserService::getCurrentLoginUserId();

		$filter = [];

		!empty($params['search']) && $filter[] = ['center.center_name|p.project_name', 'like', "%{$params['search']}%"];

		return $this->alias('c')
			->join('shiyao_project_center center', 'center.id=c.center_id')
			->join('shiyao_project p', 'p.project_id=c.project_id')
			->with(['project.company', 'center', 'pi'])->where('c.user_id', '=', $userId)
			->field('c.*,p.project_name,p.project_code')
			->where('c.is_delete', '=', 0)
			->where($filter)
			->paginate(15);
	}

	/**
	 * 批量写入
	 * @param int $goodsId
	 * @param array $specList
	 * @return array|false
	 */
	public static function increased(int $centerId, $projectId, array $crcList) {
		$password = SettingModel::getItem('trade', 10001)['order']['password'];
		Db::name('shiyao_project_center_crc')->where('project_id', $projectId)->where('center_id', $centerId)->delete();
		$dataset = [];
		foreach ($crcList as $item) {
			//新增CRc
			$userInfo = Db::name('user')->where('is_delete',0)->where('mobile', $item['phone'])->find();
			if ($userInfo) {
				$userId = $userInfo['user_id'];
				$userData['nick_name'] = $item['name'];
				$userData['grade_id'] = 1;
				
				Db::name('user')->where('user_id', $userId)->update($userData);
			} else {
				$userData['nick_name'] = $item['name'];
				$userData['mobile'] = $item['phone'];
				$userData['password'] = $password;
				$userData['grade_id'] = 1;
				$userData['create_time'] = time();
				$userData['store_id'] = self::$storeId;
				$userId = Db::name('user')->insertGetId($userData);
			}

			$data = array_merge($item, [
				'center_id' => $centerId,
				'user_id' => $userId,
				'project_id' => $projectId,
				'store_id' => self::$storeId,
			]);

			$dataset[] = $data;
		}

		return (new static )->addAll($dataset);
	}

	/**
	 * 编辑收货地址
	 * @param array $data
	 * @return bool
	 * @throws BaseException
	 */
	public function edit(array $data) {

		// 更新收货地址
		return $this->save($data) !== false;
	}

	/**
	 * 删除
	 * @return bool
	 * @throws BaseException
	 */
	public function remove() {
		// 查询当前是否为默认地址
		$user = UserService::getCurrentLoginUser(true);
		// 清空默认地址
		if ($user['address_id'] == $this['address_id']) {
			UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
		}
		// 标记为已删除
		return $this->save(['is_delete' => 1]);
	}

	/**
	 * 收货地址详情
	 * @param int $addressId
	 * @return UserAddress|array|null
	 * @throws BaseException
	 */
	public static function detail(int $projectId) {
		$userId = UserService::getCurrentLoginUserId();
		$detail = self::get(['project_id' => $projectId]);
		if (empty($detail)) {
			throwError('未找到该项目');
		}
		return $detail;
	}
}
